package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Description swagger-ui相关配置文件
 * @Version 1.0.0
 * @author zy.yang
 * @date 2021/9/6 16:13
 **/
@Configuration
@EnableSwagger2
//@Profile({"dev","test"})
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder().title("工具测试").version("V1.0").build());
    }


    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }

        /**
         * 解决swagger死循环问题
         * 视具体情况用,若控制台报错 No mapping for GET /aifruit/null/swagger-resources/configuration/security
         * 则使用下面代码
         * 下面这几个地址会一直轮询，改为重定向
         * /null/swagger-resources/configuration/ui
         * /null/swagger-resources/configuration/security
         * /null/swagger-resources
         */
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addRedirectViewController("/null/api-docs",
                    "/api-docs").setKeepQueryParams(true);
            registry.addRedirectViewController("/null/swagger-resources/configuration/ui",
                    "/swagger-resources/configuration/ui");
            registry.addRedirectViewController("/null/swagger-resources/configuration/security",
                    "/swagger-resources/configuration/security");
            registry.addRedirectViewController("/null/swagger-resources", "/swagger-resources");
        }
    }






}
